<?php

/**
 * This is the model class for table "contents".
 *
 * The followings are the available columns in table 'contents':
 * @property string $cid
 * @property string $title
 * @property string $slug
 * @property string $created
 * @property string $modified
 * @property string $text
 * @property string $order
 * @property string $authorId
 * @property string $template
 * @property string $type
 * @property string $status
 * @property string $password
 * @property string $commentsNum
 * @property string $allowComment
 * @property string $allowPing
 * @property string $allowFeed
 * @property string $parent
 */
class Contents extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Contents the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{contents}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, slug', 'required'),
			array('title, slug', 'length', 'max'=>200),
			array('slug', 'unique'),
			array('created, modified, order, authorId, commentsNum, parent', 'length', 'max'=>10),
			array('template, password', 'length', 'max'=>32),
			array('type, status', 'length', 'max'=>16),
			array('allowComment, allowPing, allowFeed', 'length', 'max'=>1),
			array('text', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('cid, title, slug, created, modified, text, order, authorId, template, type, status, password, commentsNum, allowComment, allowPing, allowFeed, parent', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'authorId'=>array(self::BELONGS_TO,'Users','authorId'),
			'categorys'=>array(self::MANY_MANY,'Metas','{{relationships}}(cid,mid)',
				'condition'=>'categorys.type=\'category\'',
			),
			'tags'=>array(self::MANY_MANY,'Metas','{{relationships}}(cid,mid)',
				'condition'=>'tags.type=\'tag\'',
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'cid' => 'Cid',
			'title' => 'Title',
			'slug' => 'Slug',
			'created' => 'Created',
			'modified' => 'Modified',
			'text' => 'Text',
			'order' => 'Order',
			'authorId' => 'Author',
			'template' => 'Template',
			'type' => 'Type',
			'status' => 'Status',
			'password' => 'Password',
			'commentsNum' => 'Comments Num',
			'allowComment' => 'Allow Comment',
			'allowPing' => 'Allow Ping',
			'allowFeed' => 'Allow Feed',
			'parent' => 'Parent',
			'categorysStr'=>'分类',
			'tagsStr'=>'标签',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->order="cid desc";

		$criteria->compare('cid',$this->cid,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('slug',$this->slug,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);
		$criteria->compare('text',$this->text,true);
		$criteria->compare('order',$this->order,true);
		$criteria->compare('authorId',$this->authorId,true);
		$criteria->compare('template',$this->template,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('commentsNum',$this->commentsNum,true);
		$criteria->compare('allowComment',$this->allowComment,true);
		$criteria->compare('allowPing',$this->allowPing,true);
		$criteria->compare('allowFeed',$this->allowFeed,true);
		$criteria->compare('parent',$this->parent,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function getCategorysStr(){
		$arr=array();
		foreach($this->categorys as $category){
			$arr[].=$category->name;
		}
		return implode(',',$arr);
	}

	public function getTagsStr(){
		$arr=array();
		foreach($this->tags as $tag){
			$arr[].=$tag->name;
		}
		return implode(',',$arr);
	}
}
